<!--
 * @Author: 孙开源 && sunkaiyuan@namenode.cn
 * @Date: 2023-02-07 08:50:24
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @LastEditTime: 2023-11-09 12:59:29
 * @Description: 
 * 
-->
<template>
	<el-dialog :title="titleMap[mode]" v-model="visible" :width="600" destroy-on-close @closed="$emit('closed')">
		<el-tabs type="border-card">
			<el-tab-pane ref="tab" label="基础信息">
				<el-form :model="form" :rules="rules" ref="dialogForm" label-width="100px" label-position="right">
					<el-row>
						<el-col :span="12">
							<el-form-item label="接口" prop="model">
								{{ form.model }}/{{ form.controller }}/{{
									form.action
								}}
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-alert type="warning" show-icon :closable="false">
								<p>关闭后，将跳过访问权限检查</p>
							</el-alert>
							<el-form-item label="访问权限" prop="is_visit">
								<el-switch v-model="form.is_visit" :active-value="1" :inactive-value="0" />
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-alert type="warning" show-icon :closable="false">
								<p>日志开启后，将产生大量的访问与返回数据！</p>
							</el-alert>
							<el-form-item label="开启日志" prop="islog">
								<el-switch v-model="form.islog" :active-value="1" :inactive-value="0" />
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-alert type="warning" show-icon :closable="false">
								<p>
									可订阅开启后，将允许将调用接口的返回数据发送给订阅的接口！
								</p>
							</el-alert>
							<el-form-item label="可订阅" prop="can_subscribe">
								<el-switch v-model="form.can_subscribe" :active-value="1" :inactive-value="0" />
							</el-form-item>
						</el-col>
						<el-alert type="error" show-icon :closable="false">
							<p>
								<b>1、</b>设置查看权限后，调用此接口的数据查询权限，将与
								角色部门 与
								数据的定义部门匹配，不一致的数据将不显示
							</p>
							<p>
								<b>2、</b>
								有多个字段，用分号";"分割
							</p>
							<p>
								<b>3、</b>
								例：location.department_id;creator.department_id
								<br />将构成php：<br />
								$query->orWhereHas(location,department_id==USER.department_id);<br />
								$query->orWhereHas(creator,department_id==USER.department_id)
							</p>
						</el-alert>
						<el-col :span="12">
							<el-form-item label="设置查看权限" prop="is_data_role">
								<el-switch v-model="form.is_data_role" :active-value="1" :inactive-value="0" />
							</el-form-item>
						</el-col>
						<el-col :span="12">
							<el-form-item label="权限部门字段" prop="department_field">
								<el-input v-model="form.department_field"></el-input>
							</el-form-item>
						</el-col>
						<el-col :span="24">
							<el-form-item label="备注" prop="remark">
								<el-input v-model="form.remark"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
			</el-tab-pane>
		</el-tabs>
		<template #footer>
			<el-button @click="visible = false">取 消</el-button>
			<el-button type="primary" :loading="isSaveing" @click="submit()">保 存</el-button>
		</template>
	</el-dialog>
</template>

<script>
export default {
	emits: ["success", "closed"],
	data() {
		return {
			mode: "add",
			titleMap: {
				add: "新增",
				edit: "编辑",
			},
			form: {},
			rules: {
				model: [{ required: true, message: "必填" }],
				controller: [{ required: true, message: "必填" }],
				action: [{ required: true, message: "必填" }],
			},
			visible: false,
			isSaveing: false,
		};
	},
	methods: {
		//显示
		open(mode = "add") {
			this.mode = mode;
			this.visible = true;
			return this;
		},
		//表单提交方法
		submit() {
			this.$refs.dialogForm.validate(async (valid) => {
				if (valid) {
					this.isSaveing = true;
					var res = await this.$API.system.api.save.post(this.form);
					this.isSaveing = false;
					if (res.code == 200) {
						this.$emit("success", this.form, this.mode);
						this.visible = false;
						this.$message.success("操作成功");
					} else {
						this.$alert(res.message, "提示", { type: "error" });
					}
				}
			});
		},
		//表单注入数据
		setData(data) {
			Object.assign(this.form, this.$TOOL.objCopy(data));
		},
	},
};
</script>

<style></style>
